package a51qindian.com.comprehensive.city;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.li.interfaces.OnItemClickListener;

import java.util.List;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.model.FunctionModel;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Li
 * Date: 2017/9/12 0012
 * Description: 工作中心功能
 */

public class FunctionAdapter extends RecyclerView.Adapter<FunctionAdapter.FunctionHolder> {


    public void setFunctionModelList(List<FunctionModel> functionModelList) {
        mFunctionModelList = functionModelList;
        notifyDataSetChanged();
    }

    private List<FunctionModel> mFunctionModelList;

    private LayoutInflater mInflater;

    public FunctionAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    private OnItemClickListener mItemClickListener;

    @Override
    public FunctionHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_function, parent, false);
        final FunctionHolder holder = new FunctionHolder(view);
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onItemClick(v, holder.getLayoutPosition());
                }
            });
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(FunctionHolder holder, int position) {
        FunctionModel model = mFunctionModelList.get(position);
        holder.mImg.setImageResource(model.getImgRes());
        holder.mTxt.setText(model.getTxt());
    }

    @Override
    public int getItemCount() {
        return mFunctionModelList == null ? 0 : mFunctionModelList.size();
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    static class FunctionHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img)
        ImageView mImg;
        @BindView(R.id.txt)
        TextView mTxt;

        public FunctionHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
